// pages/device/report/report.js

var app = getApp()
var cmd = require('../../../command/command')
var bluetooth = require('../../../command/bluetooth.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageSrc: 'https://ytouch-1258011219.cos.ap-nanjing.myqcloud.com/logo_copy.png',
    inputValue: '',
    errorDes: '正常'
  },

  intervalSendId : null,

  isCurPage(){
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    return currentPage.route === 'pages/device/report/report';    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    bluetooth.send(cmd.getCommandByName("machineWorkState"))
    bluetooth.send(cmd.getCommandByName("machineErrorState"))
    bluetooth.send(cmd.getCommandByName("appVersion"))

    this.setData({
      errorDes: cmd.getFaultCodeDesc()
    })

    this.intervalSendId = setInterval(() => {
      if(!this.isCurPage()){
        return
      }

      this.setData({
        errorDes: cmd.getFaultCodeDesc()
      })

      if(cmd.getFaultCode() == null){
        setTimeout(() => {
          bluetooth.send(cmd.getCommandByName("machineErrorState"))
        }, 30);
      }

      if(cmd.getStatusCode() == null){
        bluetooth.send(cmd.getCommandByName("machineWorkState"))
      }
      
      if(cmd.getMachineVersion() == null){
        setTimeout(() => {
          bluetooth.send(cmd.getCommandByName("appVersion"))
        }, 60);
      }
    }, 1000);
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.intervalSendId)
  },

  sendReport(){
    console.log("----", this.data.inputValue)

    
    wx.request({
      url: 'https://cqygdqkj.top/api/v1/zqwb/failure_info/',
      method: "POST",
      header: {
        "Authorization": app.globalData.headerToken
      },
      data: {
        "fault_code" : cmd.getFaultCode(),
        "fault_desc": cmd.getFaultCodeDesc(),
        "status_code": cmd.getStatusCode(),
        "status_desc": cmd.getStatusCodeDesc(),
        "user_feedback": this.data.inputValue,
        "soft_version": cmd.getMachineVersion(),
        "machine_name": app.globalData.bluetoothName
      },
      success(res){
        console.log(res)
        wx.showToast({
          title: '操作成功！',  // 标题
          icon: 'success',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
      })
      }
    })
  },

  inputChange(value){
    this.data.inputValue = value.detail.value
  }
})